package com.hu.ta.assignment5.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.hu.ta.assignment5.beans.PosTagger;

public class FilePos extends FileIO {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8908297465092213584L;
	private PosTagger tagger;

	public PosTagger getTagger() {
		return tagger;
	}

	public void setTagger(PosTagger tagger) {
		this.tagger = tagger;
	}
	
	public FilePos(String path) {
		super(path);
	}

	@Override
	public boolean read() {
		ObjectInputStream inI = null;
		FileInputStream in = null;

		try {
			in = new FileInputStream(this.getAbsoluteFile());
			inI = new ObjectInputStream(in);
			tagger = (PosTagger)inI.readObject();
		} catch (Exception exp) {
			exp.printStackTrace();
		} finally {
			try {
				inI.close();
				in.close();

				return true;

			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return false;
	}

	@Override
	public boolean write() {
		ObjectOutputStream outO = null;
		FileOutputStream out = null;

		try {
			out = new FileOutputStream(this.getAbsoluteFile());
			outO = new ObjectOutputStream(out);
			outO.writeObject(tagger);
		} catch (Exception exp) {
			exp.printStackTrace();
		} finally {
			try {
				outO.close();
				out.close();

				return true;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return false;
	}

}
